using System;
using System.ComponentModel;

namespace Mistral.Windows
{
	public static class ThreadSafeUI
	{
		public static void Invoke( EventHandler handler, object sender, EventArgs args )
		{
			if( handler != null )
			{
				foreach( EventHandler singleCast in handler.GetInvocationList() )
				{
					ISynchronizeInvoke syncInvoke = singleCast.Target as ISynchronizeInvoke;
					try
					{
						if( ( null != syncInvoke ) && ( syncInvoke.InvokeRequired ) )
							syncInvoke.Invoke( singleCast, new object[] { sender, args } );
						else
							singleCast( sender, args );
					}
					catch
					{ }
				}
			}
		}

		public static void Invoke( ISynchronizeInvoke syncInvoke, Delegate @delegate, params object[] args )
		{
			try
			{
				if( syncInvoke.InvokeRequired )
					syncInvoke.Invoke( @delegate, args );
				else
					@delegate.DynamicInvoke( args );
			}
			catch
			{ }
		}

		
	}
}